export default {
  parseURL(url) {
    var a = document.createElement('a');
    a.href = url;
    return {
      source: url,
      protocol: a.protocol.replace(':', ''),
      host: a.hostname,
      port: a.port,
      query: a.search,
      params: (function () {
        var ret = {},
          seg = a.search.replace(/^\?/, '').split('&'),
          len = seg.length, i = 0, s;
        for (; i < len; i++) {
          if (!seg[i]) {
            continue;
          }
          s = seg[i].split('=');
          ret[s[0]] = s[1];
        }
        return ret;
      })(),
      file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
      hash: a.hash.replace('#', ''),
      path: a.pathname.replace(/^([^\/])/, '/$1'),
      relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
      segments: a.pathname.replace(/^\//, '').split('/')
    };
  },
  skipFilter(link, that) {
    var currenthost = window.location.hostname;
    that.$vuet.getModule(that.$route.name).needFetch = false
    if (this.parseURL(link).host == "192.168.2.230" || this.parseURL(link).host  == "wap.test.miaohui.com"|| this.parseURL(link).host  == "wap.miaohui.com") {
      var routepath = this.parseURL(link).path;
      if (routepath == '/' || routepath == '/discovery' || routepath == '/sale' || routepath == '/shop' || routepath == '/profile') {
        that.$replaceRouteByPath(routepath)
      } else {
        that.$pushRouteByPath(this.parseURL(link).path + this.parseURL(link).query)
      }
    } else {
      if (window.location.href.indexOf(that.GLOBAL.skitToThirdWebSign) == -1) {
        window.location.replace(window.location.href + that.GLOBAL.skitToThirdWebSign)
      }
      window.location.href = link
    }
    // setTimeout(() => {
    //
    // }, 100)
    // that.$vuet.store[that.$route.name].needFetch = false

    // debugger
    // var routepath = this.parseURL(link).path;
    // if (routepath == '/' || routepath == '/discovery' || routepath == '/sale' || routepath == '/shop' || routepath == '/profile') {
    //   that.$replaceRouteByPath(routepath)
    // } else {
    //   that.$pushRouteByPath(this.parseURL(link).path + this.parseURL(link).query)
    // }
  },
  calculateCount(number){
    if (number < 10000) {
      return number
    } else if (number >= 10000){
      return Math.floor(number/10000 * 10) / 10 + "w"
    }
  },
  toast(msg = '', time = 1500) {

    var toast = document.createElement('div')
    toast.className = 'common-toast common-toast-show'
    toast.innerHTML = msg
    document.body.appendChild(toast)
    toast.style.display = 'block'
    toast.style.margin = `-${toast.offsetHeight / 2}px 0 0 -${toast.offsetWidth / 2}px`
    var timer = setTimeout(() => {
      toast.className = 'common-toast common-toast-hide'
      clearTimeout(timer)
      var timer2 = setTimeout(() => {
        document.body.removeChild(toast)
        clearTimeout(timer2)
      }, 200)
    }, time)
  },
  requestprogress() {
    var toast = document.createElement('div')
    toast.id = "myprogress";
    this.show = function () {
      toast.className = 'common-request-progress common-request-progress-show'
      document.body.appendChild(toast)
      // toast.style.margin = `-${toast.offsetHeight / 2}px 0 0 -${toast.offsetWidth / 2}px`
    };
    this.hide = function () {
      if (toast.parentNode) {
        toast.className = 'common-request-progress common-request-progress-hide'
        toast.parentNode.removeChild(toast);
        // document.body.removeChild(document.getElementById("myprogress"))
      }
    }

    // var toast = document.createElement('img')
    // toast.id = "myprogress";
    // toast.src="/static/refresh.gif"
    // toast.style="height:0.6rem;width:0.6rem;  z-index: 10000;\n" +
    //   "  top: 50%;\n" +
    //   "  left: 50%;\n" +
    //   "  margin-top: -15px;\n" +
    //   "  margin-left: -15px;\n" +
    //   "  position: absolute;"
    // this.show = function () {
    //   // toast.className = 'common-request-progress common-request-progress-show'
    //   document.body.appendChild(toast)
    //   // toast.style.margin = `-${toast.offsetHeight / 2}px 0 0 -${toast.offsetWidth / 2}px`
    // };
    // this.hide = function () {
    //   if (toast.parentNode) {
    //     // toast.className = 'common-request-progress common-request-progress-hide'
    //     toast.parentNode.removeChild(toast);
    //     // document.body.removeChild(document.getElementById("myprogress"))
    //   }
    // }
  },

  formatTime(date){
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  },
  formatNumber(n){
    n = n.toString()
    return n[1] ? n : '0' + n
  }
}
